﻿namespace Rgr

type Scene() =
    let mutable internalSeq = seq []
    member this.Figures
        with get() = internalSeq
        and set(value) = internalSeq <- value

    member this.AddFigure(p:Figure) =
        let internalSeqTest = Seq.append this.Figures [p];
        this.Figures <- internalSeqTest

    member this.AddPoint(point:Point) =
        let isFound = ref 0;   
        Seq.iter(fun (f:Figure) ->  
            if f.CheckRelationship point
            then
                f.AddPoint point;
                isFound := !isFound + 1;
            ) this.Figures;
        if !isFound < 1
        then 
           let newFigure = new Figure();
           newFigure.AddPoint(point);
           this.AddFigure(newFigure);

        if !isFound > 1
        then 
            let newFigure = new Figure();
            
            Seq.iter(fun (f:Figure) ->  
                if f.CheckRelationship point
                then
                    Seq.iter(fun (p:Point) ->
                            newFigure.AddPoint(p);
                        ) f.Points; 
                    f.Points <- seq[];
                ) this.Figures;
            this.AddFigure(newFigure);

    member this.checkPoint(p:Point) = 
        let isFound = ref 0;   
        Seq.iter(fun (f:Figure) ->  
            Seq.iter(fun (pF:Point) ->  
                if Point.AbsoluteEq(p, pF)
                then isFound := 1;
                ) f.Points;
            ) this.Figures;
        let mutable test = 0;

        if !isFound = 1
        then test <- 1
        test
